package view;

import model.User;
import util.Controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JLayeredPane;


public class ZombieFactory implements Runnable{
	
    private JLayeredPane layeredPane;
    private Controller controller;
    private boolean gameOver = false;
    private List<ArrayList<Zombie>> zombieLists = new LinkedList<>();
    ZombieFactory(JLayeredPane layeredPane, Controller controller){
        this.controller = controller;
        this.layeredPane = layeredPane;
        for (int i=0 ; i<5 ; i++) {
            zombieLists.add(new ArrayList<>());
        }
        controller.setZombieLists(zombieLists);
    }


    private Zombie newZombie(int type, int r){
        Zombie zombie = null;
        switch (type){
            case 1:
                zombie =  new Zombie(controller, r);



        }
        return zombie;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int gameProgress = 9;
        List<String> mission = new ArrayList<>();
        try {
            mission = controller.getMission(1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for ( ; !gameOver&&gameProgress<mission.size() ; gameProgress++){
            try {
                if (mission.get(gameProgress).equals(""))
                    continue;
                String []strs = mission.get(gameProgress).split("\\s+");
                if (strs[0].equals("large")){
                    //一大波
                    System.out.println("一大波僵尸");
                    continue;
                }

            	int speed = Integer.parseInt(strs[0]);
                int type = Integer.parseInt(strs[1]);
                int r = Integer.parseInt(strs[2]);
                if (r<0){
                    r = new Random().nextInt(5);
                }
                
                Thread.sleep(speed*100);
                Zombie zombie = newZombie(type, r);
                zombieLists.get(zombie.getR()).add(zombie);
                new Thread(zombie).start();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
